﻿using System;
using System.Collections.Generic;
using Adf.Core.Db;
using Adf.Core.Entity;
using Adf.Core.Util;
using CYQ.Data.Table;
using CYQ.Data.Tool;
using Test.Interface;

namespace Test.Implement
{
    public class UserImpl : IUser
    {

        private const String CurrentTableName = "adf_user";
        private const String VCurrentTableName = "adf_user";




        /// <summary>
        /// 初始化值
        /// 袁赛
        /// 2020-5-22
        /// </summary>
        /// <returns></returns>
        public MDataRow InitDataRow()
        {
            MDataRow mDataRow = new MDataRow(DBTool.GetColumns(CurrentTableName));
            return mDataRow;
        }

        /// <summary>
        /// 获得实体
        /// 袁赛
        /// </summary>
        /// <param name="sUserCode">通知编号</param>
        /// <param name="bCache">是否启用缓存</param>
        /// <returns></returns>
        public MDataRow GetEntityWithUserCode(string sUserCode, bool bCache = false)
        {
            String sWhere = "UserCode=" + DbService.SetQuotesValue(sUserCode);
            String cacheKey = "test-" + sUserCode;
            return DbService.GetOne(VCurrentTableName, sWhere, cacheKey, bCache);
        }

        /// <summary>
        /// 增加数据
        /// 袁赛
        /// </summary>
        /// <param name="dataRow"></param>
        /// 
        /// <returns></returns>
        public ExeMsgInfo Add(MDataRow dataRow)
        {
            ExeMsgInfo exeMsgInfo = new ExeMsgInfo();

            String sUserCode = dataRow["UserCode"].ToString();
            if (String.IsNullOrEmpty(sUserCode))
            {
                exeMsgInfo.RetStatus = 400;
                exeMsgInfo.RetValue = "编号不能为空";
                return exeMsgInfo;
            }
            String sWhere = "UserCode=" + DbService.SetQuotesValue(sUserCode);
            if (DbService.Exists(CurrentTableName, sWhere))
            {
                exeMsgInfo.RetStatus = 400;
                exeMsgInfo.RetValue = "编号已存在";
                return exeMsgInfo;
            }

            String controlFieldNames = "usercode,userpassword,username,orgcode,cellphone,birthdate,email,userorder,address,createdatetime,idcard,userremark,usercreatedate,qq,wx,headimg";
            exeMsgInfo = DbService.Insert(CurrentTableName, dataRow, controlFieldNames, true);
            return exeMsgInfo;
        }

        /// <summary>
        /// 更新数据
        /// 袁赛
        /// </summary>
        /// <param name="dataRow"></param>
        /// <returns></returns>
        public ExeMsgInfo Update(MDataRow dataRow)
        {
            ExeMsgInfo exeMsgInfo = new ExeMsgInfo();

            String sUserCode = dataRow["UserCode"].ToString();
            if (String.IsNullOrEmpty(sUserCode))
            {
                exeMsgInfo.RetStatus = 400;
                exeMsgInfo.RetValue = "编号不能为空";
                return exeMsgInfo;
            }
            String sWhere = "UserCode=" + DbService.SetQuotesValue(sUserCode);
            if (!DbService.Exists(CurrentTableName, sWhere))
            {
                exeMsgInfo.RetStatus = 400;
                exeMsgInfo.RetValue = "编号不存在";
                return exeMsgInfo;
            }

            String controlFieldNames = "usercode,userpassword,username,orgcode,cellphone,birthdate,email,userorder,address,createdatetime,idcard,userremark,usercreatedate,qq,wx,headimg";
            exeMsgInfo = DbService.Update(CurrentTableName, dataRow, sWhere, controlFieldNames, true);
            return exeMsgInfo;
        }





    /// <summary>
    /// 删除数据
    /// 袁赛
    /// </summary>
    /// <param name="sUserCode">通知编号</param>
    /// <returns></returns>
    public ExeMsgInfo DeleteByUserCode(string sUserCode)
    {
        throw new System.NotImplementedException();
    }

    /// <summary>
    ///获得列表
    /// 袁赛
    /// </summary>
    /// <param name="pageIndex">页号</param>
    /// <param name="pageSize">页大小</param>
    /// <param name="orderBy">排序</param>
    /// <param name="recordCount">总数</param>
    /// <param name="pageCount">总页数</param>
    /// <returns></returns>
    public MDataTable GetList(int pageIndex, int pageSize, string orderBy, ref int recordCount, ref int pageCount)
    {
        String sWhere = " 1=1";

        if (String.IsNullOrEmpty(orderBy))
        {
            orderBy = " order by  userid desc";
        }
        sWhere += orderBy;
        return DbService.GetPageTable(VCurrentTableName, pageIndex, pageSize, sWhere, ref recordCount, ref pageCount);

        }
    }
}